<?php 

header('Content-Type: text/html;charset=UTF-8');

$today = date('d-m-Y');

$tomorrow = date("Y.m.d", mktime(0, 0, 0, 7, date("d")+1,  date("Y")));

//echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 
//echo date('N', mktime(0, 0, 0, date("m"), 1,  date("Y")));



// Détecter la date d'aujourd'hui pour la mettre en valeur
// Design




class Calendar
{

	protected $current_month;
	protected $nb_weeks;
	protected $first_day;
	protected $last_day;
	
	protected $prev_month;
	protected $prev_last_day;
	
	protected $next_month;
	
	public function __construct($date){
	
		$this->current_month = $this->month($date); // date du premier jour du mois
		$this->nb_weeks = $this->weeksOnMonth($date);
		$this->first_day = $this->firstDayOfMonth($date);
		$this->last_day = $this->daysByMonth($date);
		
		$this->prev_month = $this->previousMonth($date);
		$this->prev_last_day = $this->daysByMonth($this->prev_month);
		
		$this->next_month = $this->nextMonth($date) ;
	
	}
	
	protected function firstDayOfMonth($date){
		
		$exploded_date = explode("-", $date);
		return date('N', mktime(0, 0, 0, $exploded_date[1], 1, $exploded_date[2]));
		//retourne le jour de la semaine du premier jour du mois.
	}
	
	protected function daysByMonth($date){
		
		$exploded_date = explode("-", $date);
		return date('t', mktime(0, 0, 0, $exploded_date[1], 1,  $exploded_date[2]));	
		//return nombre de jours dans le mois
	}
		
	protected function weeksOnMonth($date){
	
		$exploded_date = explode("-", $date);
		$last_day = $this->daysByMonth($date);
		$first_week = date('W', mktime(0, 0, 0, $exploded_date[1], 1,  $exploded_date[2]));
		$last_week = date('W', mktime(0, 0, 0, $exploded_date[1], $last_day,  $exploded_date[2]));
		
		if ($first_week == 52){
			return $last_week+1;
		}else{
			return $last_week-$first_week+1;
		}
		//return nombre de semaines dans le mois
	}
	
	protected function month($date){ // retourne le premier jour du mois 

		$exploded_date = explode("-", $date);

		return date('d-m-Y', mktime(0, 0, 0, $exploded_date[1], 1,  $exploded_date[2]));

	}
	
	protected function previousMonth($date){
	
		$exploded_date = explode("-", $date);
		
		if ($exploded_date[1] == '01'){
			return date('d-m-Y', mktime(0, 0, 0, 12, 1,  $exploded_date[2]-1));
		}else{
			return date('d-m-Y', mktime(0, 0, 0, $exploded_date[1]-1, 1, $exploded_date[2]));
		}
	
	}
	
	protected function nextMonth($date){
	
		$exploded_date = explode("-", $date);
		
		if ($exploded_date[1] == '12'){
			return date('d-m-Y', mktime(0, 0, 0, 1, 1,  $exploded_date[2]+1));
		}else{
			return date('d-m-Y', mktime(0, 0, 0, $exploded_date[1]+1, 1,  $exploded_date[2]));
		}
	
	}
	
	
	protected function checkColumn(&$day){
	
		if($day >= 7){
	
			echo '</tr>';
			
			$day = 1;
		
		}else{
	 
			$day++;
		
		}
		
	}
	
	
	protected function currentDate($month, $num_day){
	
		$exploded_date = explode("-", $month);
	
		return date('d-m-Y', mktime(0, 0, 0, $exploded_date[1], $num_day,  $exploded_date[2]));
	
	}
	
	protected function textMonth($date){
	
		$exploded_date = explode("-", $date);
		return date('F Y', mktime(0, 0, 0, $exploded_date[1], $exploded_date[0], $exploded_date[2]));

	}
	
	protected function isToday($date){
	
		if(date('d-m-Y') == $date){
			return $date;
		}else {
			return '';
		}
	
	}
	
	protected function howManyEvents($link, $date){
	
		$query = "SELECT COUNT(id_event) as count FROM event WHERE date BETWEEN DEBUT AND FIN";
		$query2 = "SELECT COUNT(id_task) as count FROM task WHERE end_date = madate";
		
		$result = mysqli_query($link, $query);
		$result2 = mysqli_query($link, $query2);

		$count = 0;
		
		while($row = mysqli_fetch_array($result)){
			$count += $row['count'];
		}
		
		while($row = mysqli_fetch_array($result2)){
			$count += $row['count'];
		}
		
		if($count > 0){
			return '<div class="event">'.$count.'</div>';
		}else{
			return "";
		}
		
	}
	
	public function createCalendar(){
	
		$link = mysqli_connect('localhost', 'root', '', 'simplyproject');

		if(mysqli_connect_error()){
			die('Impossible de se connecter à la base de données : '.mysqli_connect_errno().' - '.mysqli_connect_error());
		}

		
		$day = 1; // jour de la semaine
		$num_row = 1;
		$total_rows = $this->get_nb_weeks();
		$nb_days = $this->get_last_day(); // nombre de jours dans le mois courant
		
		$prev_anchor = explode('-', $this->get_prev_month());
		$next_anchor = explode('-', $this->get_next_month());
		
		
		echo '<a href="#'.$prev_anchor[1].'-'.$prev_anchor[2].'" id="prev_month" ><img src="ressources/fleche_g.png" alt="mois précedent" /></a>';
		echo '<h1>'.$this->textMonth($this->get_current_month()).'</h1>';
		echo '<a href="#'.$next_anchor[1].'-'.$next_anchor[2].'" id="next_month" ><img src="ressources/fleche_d.png" alt="mois suivant" /></a>';

		
		echo '<table class="calendar">';
		echo '<thead><th>Lundi</th><th>Mardi</th><th>Mercredi</th><th>Jeudi</th><th>Vendredi</th><th>Samedi</th><th>Dimanche</th>';
		echo '<tbody><tr>';
		
		for($i = $this->get_first_day(); $i > 1; $i--){
			
			$current_day = $this->get_prev_last_day()-($i-2); // numero du jour courant du mois courant
			
			$current_date = $this->currentDate($this->prev_month, $current_day); // format AAAA.MM.JJ
			
			echo '<td class="prev_month day'.$this->isToday($current_date).'" id="'.$current_date.'">'.$current_day.'</td>';
			
			$this->checkColumn($day); // ajoute un </tr> au bout du 7eme jour
		
		}
		
		for($i = 1; $i <= $nb_days; $i++){
			
			if($day==1){
				echo '<tr>';
			}
			
			
			$current_date = $this->currentDate($this->current_month, $i); // format AAAA.MM.JJ
						
			echo '<td class="current_month day'.$this->isToday($current_date).'" id="'.$current_date.'">'.$this->howManyEvents($link).'<div class="num_day">'.$i.'</div>'.'</td>';
			
			//'<div class="event">2</div>'
			
			$this->checkColumn($day);
			
			//echo $num_row;
		}
		

			for($i = 1; ($i <= 8-$day)&&($day>1); $i++){
				
				$current_date = $this->currentDate($this->next_month, $i); // format AAAA.MM.JJ
				
				echo '<td class="next_month day'.$this->isToday($current_date).'" id="'.$current_date.'">'.$i.'</td>';
				
				if(($i+$day) > 7){
					echo '</tr>';	
				}
			
			}
		
		echo '</tbody></table>'; 
		
	}
	
	
	public function get_current_month(){
		return $this->current_month;
	}
	
	public function get_nb_weeks(){
		return $this->nb_weeks;
	}
	
	public function get_first_day(){
		return $this->first_day;
	}
	
	public function get_last_day(){
		return $this->last_day;
	}
	
	public function get_prev_month(){
		return $this->prev_month;
	}
	
	public function get_prev_last_day(){
		return $this->prev_last_day;
	}
	
	public function get_next_month(){
		return $this->next_month;
	}	
	
}

//$calendrier = new Calendar($_GET['date']);
$calendrier = new Calendar($today);
$calendrier->createCalendar();


		

	


?>